﻿namespace KeRui.Common
{
    using System;
    using System.Text;

    public class CalUtility
    {
        private int iCount = 0;
        private int iCurr = 0;
        private StringBuilder StrB;

        public CalUtility(string calStr)
        {
            this.StrB = new StringBuilder(calStr.Trim());
            this.iCount = Encoding.Default.GetByteCount(calStr.Trim());
        }

        public bool Compare(string str1, string str2)
        {
            return (this.getPriority(str1) >= this.getPriority(str2));
        }

        public string getItem()
        {
            if (this.iCurr == this.iCount)
            {
                return "";
            }
            char c = this.StrB[this.iCurr];
            bool flag = this.IsNum(c);
            if (!flag)
            {
                this.iCurr++;
                return c.ToString();
            }
            string str = "";
            while ((this.IsNum(c) == flag) && (this.iCurr < this.iCount))
            {
                c = this.StrB[this.iCurr];
                if (this.IsNum(c) != flag)
                {
                    return str;
                }
                str = str + c;
                this.iCurr++;
            }
            return str;
        }

        public int getPriority(string str)
        {
            if (str.Equals(""))
            {
                return -1;
            }
            if (!str.Equals("("))
            {
                if (str.Equals("+") || str.Equals("-"))
                {
                    return 1;
                }
                if (str.Equals("*") || str.Equals("/"))
                {
                    return 2;
                }
                if (str.Equals(")"))
                {
                    return 0;
                }
            }
            return 0;
        }

        public bool IsNum(char c)
        {
            return (((c >= '0') && (c <= '9')) || (c == '.'));
        }

        public bool IsNum(string c)
        {
            if (c.Equals(""))
            {
                return false;
            }
            return (((c[0] >= '0') && (c[0] <= '9')) || (c[0] == '.'));
        }
    }
}

